package com.config.client.web;

import com.config.client.util.HttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;

/**
 * Created by liuaiguo on 2016/12/28.
 */
@RestController
public class Controller {
    @Value("${active}")
    private String active;

    @Autowired
    private Environment env;

    @RequestMapping(value = "/getPro")
    public String getPro(){
        return env.getProperty("active");
    }

    //刷新配置
    @RequestMapping(value = "/fresh")
    public String refresh(HttpServletRequest request) throws MalformedURLException{
        String host = "http://" + request.getServerName() + ":" + request.getServerPort();
        if (StringUtils.isNotBlank(host)){
            String freshUrl = host + "/refresh";
            HttpRequest.sendPost(freshUrl,null);
            return "success";
        }

        return "fail";
    }
}
